package com.yanqu.road.server.manger.kowloon.battle.role.bullet;

import com.yanqu.road.server.manger.kowloon.battle.KowloonGame;
import com.yanqu.road.server.manger.kowloon.battle.role.enemy.KowloonEnemy;

import java.util.ArrayList;

public class KowloonBulletAmulet extends KowloonBullet{

    private int atkNum;

    private boolean isAmulet;

    public KowloonBulletAmulet(KowloonGame game) {
        super(game);
    }

    @Override
    public void init() {
        this.atkNum = this.shooter.data.getProperty().getAttackNum();
        this.enemyIds = new ArrayList<>();
        this.isAmulet = true;
    }

    @Override
    public void updatePos(double dt) {
        int speed = this.shooter.data.getProperty().getMoveSpeed();
        double angle = Math.atan2(this.target.pos.getY() + this.target.getHurtRect().getHeight() / 2 - this.pos.getY(),
                this.target.pos.getX() - this.pos.getX());
        this.speed.setX(speed * Math.cos(angle));
        this.speed.setY(speed * Math.sin(angle));
        this.angle = angle;
        super.updatePos(dt);
    }

    @Override
    public void updateAI(double dt) {
        if (this.target.isAlive()) {
            if (this.target.getHurtRect().contains(this.pos)) {
                if (this.isAmulet) {
                    if (game.roll(this.shooter.data.getProperty().getVertigoBuff().getProbability())) {
                        this.target.addBuffByBullet(this);
                    }
                }
                this.target.beHurt(this.bulletType, +this.shooter.data.getProperty().getAttack());
                this.enemyIds.add(this.target.id);
                this.atkNum--;
                if (this.atkNum > 0) {
                    KowloonEnemy enemy = this.getNearEnemy(this.shooter.data.getProperty().getEjectionRange());
                    if (enemy != null) {
                        this.target = enemy;
                        this.isAmulet = false;
                    } else {
                        this.destroy();
                    }
                } else {
                    this.destroy();
                }
            }
        } else {
            this.destroy();
        }
    }
}
